package com.syq.service.impl;

import com.syq.constant.StrategyConstants;
import com.syq.feign.CacheFeign;
import com.syq.model.StandardSubmit;
import com.syq.service.FilterChainService;
import com.syq.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Set;

/**
 * @ClassName: LimitiFiveMinutesFilterServiceImpl
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/27 9:49
 */

@Service("limitOneHour")
@Slf4j
public class LimitFiveOneHourServiceImpl implements FilterChainService {
    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private PushMsgUtil pushMsgUtil;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        System.out.println("一小时");
        Integer clientID = standardSubmit.getClientID();
        String destMobile = standardSubmit.getDestMobile();
        //当前时间毫秒值
        long l = System.currentTimeMillis();
        //五分钟之前的毫秒值
        long before=l-60*60*1000;

        String key=clientID+":"+destMobile;
        Set<String> value = cacheFeign.zRangeByScore(key, before, l);
        if (!StringUtils.isEmpty(value)&&value.size()>=5){
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            pushMsgUtil.pushReport(standardSubmit, StrategyConstants.STRATEGY_ERROR_LIMIT);
            log.error("一小时之内禁止发送超过五条");
            return false;
        }
        cacheFeign.zadd(key,String.valueOf(l),l);
        return true;
    }
}
